package com.aaa.service.impl;

import com.aaa.dao.UserDao;
import com.aaa.entity.Role;
import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    public User checkUser(User user) {
        return userDao.checkUsernameAndPassword(user);
    }

    public TableData<User> listPage(String keyword,Integer page, Integer limit) {

        int offset = (page - 1) * limit;
        List<User> data = userDao.listPage(keyword,offset, limit);
        Integer count = userDao.count();
        TableData<User> tableData=new TableData<User>();
        tableData.setCount(count);
        tableData.setData(data);

        return tableData;
    }

    public DefaultMsg saveOrUpdate(User user) {

        Date date=new Date();
        //日期格式化                                                     可以换为年月日  时分秒
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        user.setCreate_date(format);
        Integer count;
        if(user.getId()==null){
            count = userDao.save(user);
        }else{
            count = userDao.update(user);
        }
        DefaultMsg defaultMsg=new DefaultMsg();
        if(count==0){
            defaultMsg.setError("操作失败");
        }
        return defaultMsg;
    }

    public DefaultMsg checkUsername(String username,Integer id) {

        DefaultMsg defaultMsg=new DefaultMsg();
        if(id==null){

            Integer count = userDao.checkUsername(username);
            if(count>0){
                defaultMsg.setError("用户名重复");
            }
        }else{
            Integer count = userDao.checkUsername(username,id);
            if(count>0){
                defaultMsg.setError("用户名重复");
            }
        }

        return defaultMsg;
    }

    public DefaultMsg delete(Integer id) {
        Integer count = userDao.delete(id);
        DefaultMsg defaultMsg=new DefaultMsg();
        if(count==0){
            defaultMsg.setError("删除失败");
        }
        return defaultMsg;
    }

    public DefaultMsg addRole(User user) {
        DefaultMsg defaultMsg = new DefaultMsg();
        List<Role> roles = user.getRoles();
        Integer count = 0;

        Integer integer = userDao.deleteRole(user.getId());

        for (Role role : roles) {
            if (role.getId() != null) {
                count = userDao.saveRole(user.getId(), role.getId());
            }

        }
        if (count == null) {
            defaultMsg.setSuccess(0);
            defaultMsg.setError("更改职位失败");
        }
        return defaultMsg;
    }

    public DefaultMsg updatePassword(User user) {
        Integer count=userDao.updatePassword(user);
        DefaultMsg defaultMsg=new DefaultMsg();
        if(count==null){
            defaultMsg.setError("密码修改失败");
        }
        return defaultMsg;
    }
}
